package com.school.admin.common.filter;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Arrays;
import java.util.regex.Pattern;


@Configuration
public class AppAuthConfiguration extends WebMvcConfigurerAdapter {

    //关键，将拦截器作为bean写入配置中
    @Bean
    public AuthInterceptor getSecurityInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        InterceptorRegistration ir = registry.addInterceptor(getSecurityInterceptor());
        // 配置拦截的路径
        ir.addPathPatterns("/**");
        // 配置不拦截的路径
        ir.excludePathPatterns("**/swagger-ui.html");
        ir.excludePathPatterns("**/swagger-resources/**");
        ir.excludePathPatterns("**/swagger-resources");
        ir.excludePathPatterns("**/doc.html");
        // 还可以在这里注册其它的拦截器
//        registry.addInterceptor(new AppAuthInterceptor()).addPathPatterns("/api/**");
    }

    public static void main(String[] args) {

        for (String s : Arrays.asList("/swagger-resources/configuration/security", "/re1e/configuration/security")) {
            boolean matches = Pattern.matches("^.*(swagger|reee).*$", s);
            System.out.println(matches);
        }



    }

}
